package com.gc.monitor.action;

import com.alibaba.fastjson.JSONObject;
import com.gc.gpsoo.beans.GpsooCarData;
import com.gc.monitor.gctask.GpsooMonitorResult;
import com.gc.common.util.ReturnJsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 瑱彬 on 2015/4/15.
 */
@Controller
@RequestMapping("/gpsoo")
public class GpsooAdapterController {

    @RequestMapping("/monitor")
    @ResponseBody
    public JSONObject monitor() {
        List<GpsooCarData> gpsooCarDatas = GpsooMonitorResult.getInstance().getMonitorResults();
        return ReturnJsonUtil.successRows(gpsooCarDatas);
    }

    @RequestMapping("/tracking")
    @ResponseBody
    public JSONObject tracking(String imeis) {
        List<GpsooCarData> gpsooCarDatas;
        if (null == imeis || ("").equals(imeis)) {
            gpsooCarDatas = GpsooMonitorResult.getInstance().getMonitorResults();
        } else {
            String imeiArrays[] = imeis.split(",");
            Map<String, GpsooCarData> gpsooCarDataMap = GpsooMonitorResult.getInstance().getMonitorMap();
            gpsooCarDatas = new ArrayList<>();
            for (String imei : imeiArrays) {
                gpsooCarDatas.add(gpsooCarDataMap.get(imei));
            }
        }
        return ReturnJsonUtil.successRows(gpsooCarDatas);
    }

}
